package com.splic.web.controller.product;

import java.util.Date;
import java.util.List;

import com.splic.common.utils.DateUtils;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.Channel;
import com.splic.product.domain.Device;
import com.splic.product.domain.Product;
import com.splic.product.domain.User;
import com.splic.product.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.splic.common.annotation.Log;
import com.splic.common.enums.BusinessType;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.common.core.page.TableDataInfo;

/**
 * 用户设备Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/device")
public class DeviceController extends BaseController
{
    private String prefix = "product/device";

    @Autowired
    private DeviceService deviceServiceImpl;

    @Autowired
    private UserService userServiceImpl;

    @Autowired
    private ProductService productServiceImpl;

    @Autowired
    private DeviceStatisService deviceStatisServiceImpl;
    @Autowired
    private ChannelService channelServiceImpl;


    @RequiresPermissions("product:device:view")
    @GetMapping()
    public String device(ModelMap mmap)
    {
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        List<Channel> channelList = channelServiceImpl.selectChannelList(null);
        mmap.put("channelList",channelList);
        List<String> phoneBrandList = deviceStatisServiceImpl.selectAddPhoneBrandList();
        mmap.put("phoneBrandList",phoneBrandList);
        List<String> phoneModelList = deviceStatisServiceImpl.selectAddPhoneModelList();
        mmap.put("phoneModelList",phoneModelList);
        return prefix + "/list";
    }

    /**
     * 查询用户设备列表
     */
    @RequiresPermissions("product:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceServiceImpl.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 新增用户设备
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id)){
            mmap.put("device", deviceServiceImpl.selectDeviceById(id));
        }else{
            mmap.put("device", new Device());
        }
        return prefix + "/form";
    }


    /**
     * 新增保存用户设备
     */
    @RequiresPermissions("product:device:edit")
    @Log(title = "用户设备", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Device device)
    {
        return toAjax(deviceServiceImpl.saveOrUpdate(device));
    }

    /**
     * 添加设备为黑名单设备
     */
    @GetMapping("/addBlackList")
    @ResponseBody
    public AjaxResult addBlackList(Long id)
    {
        return toAjax(deviceServiceImpl.updateDeviceIsBlackList(id));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:device:remove")
    @Log(title = "用户设备", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(deviceServiceImpl.deleteById(id));
    }


    /**
     * 导出用户设备列表
     */
    @RequiresPermissions("product:device:export")
    @Log(title = "用户设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Device device)
    {
        List<Device> list = deviceServiceImpl.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        return util.exportExcel(list, "用户设备数据");
    }

    /**
     * 查看用户信息
     */
    @GetMapping(value = { "/getUserInfo/{id}"})
    public String viewThirdBind(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        User user = userServiceImpl.selectUserById(id);
        if (user == null) {
            mmap.put("user", new User());
            return prefix + "/userinfoNull";
        } else {
            mmap.put("user", user);
        }
        return prefix + "/userinfo";
    }

    /**
     * 查看设备信息
     */
    @GetMapping(value = { "/getDeviceInfo/{id}"})
    public String viewDeviceInfo(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        Device device = deviceServiceImpl.selectDeviceById(id);
        if (device == null) {
            mmap.put("device", new Device());
            return prefix + "/deviceinfoNull";
        } else {
            mmap.put("device", device);
        }
        return prefix + "/deviceinfo";
    }

    /**
     * 已安装应用名称列表
     */
    @GetMapping(value = { "/productList/{id}"})
    public String productList(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/namelist";
    }

    /**
     * 已安装应用名称列表
     */
    @PostMapping(value = { "/getProductList"})
    @ResponseBody
    public TableDataInfo getProductList(Long id)
    {
        List<Product> nameList = productServiceImpl.selectProductByDeviceId(id);
        return getDataTable(nameList);
    }

    /**
     * 新增或修改保存其他服et务与卡群组绑定
     */
    @GetMapping("/addChangeProduct")
    @ResponseBody
    public List<String> addChangeProduct(Long productId)
    {
        List<String> productVersionList = deviceStatisServiceImpl.selectAddProductVersionList(productId);
        return productVersionList;
    }
}
